package top.lyc829.zzl_android.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.lifecycle.ViewModelProvider;

import com.jeremyliao.liveeventbus.LiveEventBus;

import java.io.IOException;

import okhttp3.Response;
import top.lyc829.zzl_android.databinding.ActivityLoginBinding;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.FormatUtil;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;
import top.lyc829.zzl_android.ui.BaseActivity;
import top.lyc829.zzl_android.ui.regist.RegistActivity;

public class LoginActivity extends BaseActivity {
    private LoginViewModel viewModel;

    // viewBinding
    private ActivityLoginBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
//        setContentView(R.layout.activity_login);
        viewModel= new ViewModelProvider(this).get(LoginViewModel.class);

        viewModel.getUserInfoLiveData().observe(this, userInfoBean -> {
            Utils.insertToSp("user_id", userInfoBean.getId().toString());
            Utils.showToast("登录成功");

            sendLiveEvent();

            finish();
        });


        binding.mTitleBarLogin.setLeftClickListener(v -> {
            finish();
        });

        binding.btnLogin.setOnClickListener(v -> {

            Utils.Sout("click login button");


            String username = binding.etPhoneNumber.getEditValue().trim();
            String password = binding.etPassword.getEditValue().trim();

            if (Utils.isStringEmpty(username)) {
                Utils.showToast("用户名不能为空");
                return;
            } else if (Utils.isStringEmpty(password)) {
                Utils.showToast("密码不能为空");
                return;
            }
            if (!FormatUtil.isEmail(username)) {
                Utils.showToast("邮箱格式错误");
                return;
            }


            Network.doLogin(username, password, new HttpUtil.MyCallback() {
                @Override
                public void success(Response res) throws IOException {
                    String resultStr = res.body().string();
                    runOnUiThread(() -> {
                        ResultBean<String> resultBean = JsonUtilNew.fromJsonObject(resultStr, String.class);
                        if (resultBean.getCode().equals(200)) {
                            viewModel.getUserInfo();
                        } else if (resultBean.getCode().equals(201)) {
                            Utils.showToast("账户未注册");
                            failed(new IOException(resultBean.getMsg()));
                        } else if (resultBean.getCode().equals(202)) {
                            Utils.showToast("密码错误");
                            failed(new IOException(resultBean.getMsg()));
                        }
                        else {
                            failed(new IOException(resultBean.getMsg()));
                        }
                    });



                }

                @Override
                public void failed(IOException e) {
                    Utils.Sout("登录失败：" + e.getMessage());
                }
            });

        });


        // 跳转到注册界面
        binding.tvRegist.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
            startActivity(intent);
        });

    }

    private void sendLiveEvent() {

//        Map<String, String> map = new HashMap<>();
//        map.put("logout", "1");

        LiveEventBus
                .get("login")
                .post(1);
    }



}